package org.jb.coinche.server.business;

import java.util.Comparator;
import java.util.List;

import org.jb.coinche.client.business.model.Card;
import org.jb.coinche.client.business.model.Card.Color;

/**
 * 
 * @author jeremie
 *
 */
public class ColorComparator implements Comparator<Card> {

	final private Card.Color trump;
	final private Card.Color askedColor;

	
	/*
	 * 
	 */
	public ColorComparator( Card.Color trump, Card.Color askedColor ) {
		this.trump = trump;
		this.askedColor = askedColor;
	}
	
	/*
	 * 
	 */
	public ColorComparator() {
		this.trump = null;
		this.askedColor = null;
	}
	
	/*
	 * 
	 */
	public ColorComparator(Color trump) {
		this.trump = trump;
		this.askedColor = null;
	}

	/*
	 * 
	 */
	public Card.Color getTrump() {
		return trump;
	}
	
	/*
	 * 
	 */
	public Card.Color getAskedColor() {
		return askedColor;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(Card o1, Card o2) {
		Card.Color c1 = o1.getColor();
		Card.Color c2 = o2.getColor();
		
		if (c1.equals(c2)) {
			
			List<Card.Value> values;
			if (trump != null && c1.equals(trump))
				values = Constant.trumpValues;
			else
				values = Constant.defaultValues;
			
			int v1 = values.indexOf( o1.getValue() );
			int v2 = values.indexOf( o2.getValue() );
			
			if (v1 == v2)
				return 0;
			
			if (v1 < v2)
				return -1;
			
			return 1;
		} else {
			
			if (c1.equals(trump))
				return -1;
			
			if (c2.equals(trump))
				return 1;
			
			if (c1.equals(askedColor))
				return -1;
			
			if (c2.equals(askedColor))
				return 1;
			
			return c1.compareTo(c2);
		}
	}
	

}